package cn.iocoder.yudao.module.blog.controller.admin.spacelike;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.spacelike.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.spacelike.BlogSpaceLikeDO;
import cn.iocoder.yudao.module.blog.convert.spacelike.BlogSpaceLikeConvert;
import cn.iocoder.yudao.module.blog.service.spacelike.BlogSpaceLikeService;

@Tag(name = "管理后台 - 动态点赞")
@RestController
@RequestMapping("/blog/space-like")
@Validated
public class BlogSpaceLikeController {

    @Resource
    private BlogSpaceLikeService spaceLikeService;

    @PostMapping("/create")
    @Operation(summary = "创建动态点赞")
    @PreAuthorize("@ss.hasPermission('blog:space-like:create')")
    public CommonResult<Long> createSpaceLike(@Valid @RequestBody BlogSpaceLikeCreateReqVO createReqVO) {
        return success(spaceLikeService.createSpaceLike(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新动态点赞")
    @PreAuthorize("@ss.hasPermission('blog:space-like:update')")
    public CommonResult<Boolean> updateSpaceLike(@Valid @RequestBody BlogSpaceLikeUpdateReqVO updateReqVO) {
        spaceLikeService.updateSpaceLike(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除动态点赞")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:space-like:delete')")
    public CommonResult<Boolean> deleteSpaceLike(@RequestParam("id") Long id) {
        spaceLikeService.deleteSpaceLike(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得动态点赞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:space-like:query')")
    public CommonResult<BlogSpaceLikeRespVO> getSpaceLike(@RequestParam("id") Long id) {
        BlogSpaceLikeDO spaceLike = spaceLikeService.getSpaceLike(id);
        return success(BlogSpaceLikeConvert.INSTANCE.convert(spaceLike));
    }

    @GetMapping("/list")
    @Operation(summary = "获得动态点赞列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:space-like:query')")
    public CommonResult<List<BlogSpaceLikeRespVO>> getSpaceLikeList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogSpaceLikeDO> list = spaceLikeService.getSpaceLikeList(ids);
        return success(BlogSpaceLikeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得动态点赞分页")
    @PreAuthorize("@ss.hasPermission('blog:space-like:query')")
    public CommonResult<PageResult<BlogSpaceLikeRespVO>> getSpaceLikePage(@Valid BlogSpaceLikePageReqVO pageVO) {
        PageResult<BlogSpaceLikeDO> pageResult = spaceLikeService.getSpaceLikePage(pageVO);
        return success(BlogSpaceLikeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出动态点赞 Excel")
    @PreAuthorize("@ss.hasPermission('blog:space-like:export')")
    @OperateLog(type = EXPORT)
    public void exportSpaceLikeExcel(@Valid BlogSpaceLikeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogSpaceLikeDO> list = spaceLikeService.getSpaceLikeList(exportReqVO);
        // 导出 Excel
        List<BlogSpaceLikeExcelVO> datas = BlogSpaceLikeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "动态点赞.xls", "数据", BlogSpaceLikeExcelVO.class, datas);
    }

}
